﻿#include "stdafx.h"
#ifdef LK_TYPE_
namespace LK
{
	namespace Reflection {
		PString GetFullClassNameById(index_t id);
		PString GetClassNameById(index_t id);
	}

	PString& CommonTypeFunctionInfoStruct::GetNameRef()
	{
		return (PString&)Name;
	}

	PMObjects Type::SameTypeReferenceConvert(PType const& to, PMObjects const& src)
	{
		if (to->IsLValueReference() == src->GetInnerType()->IsLValueReference() || to->IsArray() || (to->IsFunction() && to->GetID() != 1))return src;
		//if (!to.IsConst())throw PTypesConvertErrorException(L"无法将类型"+ src.GetInnerType().GetFullName()+L"转换到类型"+ to.GetFullName());
#ifdef _CLR
		if (src->GetInnerType()->IsGCType() && src->GetInnerType()->IsLValueReference())
		{
			gcroot<System::Object^> tmp = *(System::Object^*)src.GetObjectPtr();
			return MObjects::MakeMObjectByTypeInfo(&(void*&)tmp, to);
		}
#endif
		//生成副本
		return MObjects::MakeMObjectByTypeInfo(MObjects::GetObjectPtr(src), to);
	}
	PMObjects Type::SameTypeReferenceConvert(PType const& to, ptr<Object> const& src)
	{
		if (ReferenceEqual(src->GetType(), Make<PMObjects>()))return SameTypeReferenceConvert(to, (PMObjects&)src);
		if (to->IsLValueReference())
		{
			const void*ptr = &src;
			return MObjects::MakeMObjectByTypeInfo(to, &ptr);
		}
		return MObjects::MakeMObjectByTypeInfo(&src, to);
	}
	bool operator==(PType const&t1, PType const &t2) { return t1.GetPtr() == t2.GetPtr() || (t1.GetPtr() && t2.GetPtr() && *t1.GetPtr() == *t2.GetPtr()); }
	bool operator!=(PType const&t1, PType const &t2) { return !(t1 == t2); }
	bool operator>=(PType const&t1, PType const &t2) { return t1->CompareWith(t2) >= 0; }
	bool operator<=(PType const&t1, PType const &t2) { return t1->CompareWith(t2) <= 0; }
	bool operator>(PType const&t1, PType const &t2) { return t1->CompareWith(t2) > 0; }
	bool operator<(PType const&t1, PType const &t2) { return t1->CompareWith(t2) < 0; }
	void Type::AddTypeInfo()
	{
#ifdef LK_REFLECTION_
		using namespace Reflection;
		using namespace Functional;
		//获取全局命名空间
		PNamespaceNode space = GetGlobleLibrary().GetGlobleNamespaceRef().FindOrAddNamespace(L"LK");
		//查找或添加ptr<Time>类型节点
		PTypeNode node = space.FindOrAddTypeNode(L"Type", Type::Make<PType>());
		//添加成员函数
		//node.AddFunction(L"GetName", PFunObjs((PString(PType::*)()const)&PType::ToString));
		//node.AddFunction(L"ToString", PFunObjs((PString(PType::*)()const)&PType::ToString));
		//设置继承关系
		node.AddBaseType(Type::Make<ptr<Object>>());
#endif
	}

	index_t Type::CompareWithoutExtents(PType const&t)const
	{
		if (ID != t.GetPtr()->ID)return ID > t.GetPtr()->ID ? 1 : -1;
#ifdef _CLR
		if (ID == 18 || ID == 19)return t.Check()->gctype->Equals( gctype) ? 0 : 1;
#endif
		if (props.size() != t.Check()->props.size())
		{
			if (props.size() == 0)return -1;
			if (t.GetPtr()->props.size() == 0)return 1;
		}
		index_t i = 0;
		index_t len = props.size() > t.GetPtr()->props.size() ? t.GetPtr()->props.size() : props.size();
		while (i<len) { if (props[i] != t.GetPtr()->props[i])return props[i]>props[i] ? 1 : -1; ++i; }
		if (len < (index_t)props.size())return 1;
		if (len < (index_t)t.GetPtr()->props.size())return -1;
		if (funArgs.size() != t.GetPtr()->funArgs.size())
		{
			if (funArgs.size() == 0)return -1;
			if (t.GetPtr()->funArgs.size() == 0)return 1;
		}
		len = funArgs.size() > t.GetPtr()->funArgs.size() ? t.GetPtr()->funArgs.size() : funArgs.size();
		i = 0; while (i<len) { if (funArgs[i] != t.GetPtr()->funArgs[i])return funArgs[i]>funArgs[i] ? 1 : -1; ++i; }
		if (len < (index_t)funArgs.size())return 1;
		if (len < (index_t)t.GetPtr()->funArgs.size())return -1;
		return 0;
	}
	index_t Type::CompareWith(PType const&t)const
	{//按指针数组级-fun-id-外层特性这样的顺序进行比较
		index_t cmp = CompareWithoutExtents(t);
		if (cmp != 0)return cmp;
		if (attr != t.GetPtr()->attr)return attr>t.GetPtr()->attr ? 1 : -1;
		return 0;
	}

	ptr<Object> Type::DynamicConvertFrom(ptr<Object> const&from, index_t lv)const
	{
		//获取对象实际类型
		PType type = from->GetType();
		//记录所表示类型对象指针
		const void* objptr;
		//如果类型相同，则进行基本转换
		if (CompareWithoutExtents(type) == 0){
			//完全一样直接返回
			if (attr == type.GetPtr()->attr)
				return from;
			//记录地址
			objptr = &from;
			//GetType()所得attribute应该是空，有attr的内容应该使用封箱
			if (IsLValueReference())
				return MObjects::MakeMObjectByTypeInfo((Type*)this, &objptr);
			return MObjects::MakeMObjectByTypeInfo((Type*)this, objptr);
		}
		//非封箱对象，必从ptr<Object>派生
		if (type->CompareWithoutExtents(Type::Make<PMObjects>()) != 0)
		{
			//如果lv>0则查找转换列表
			if (lv > 0) {
#ifdef LK_REFLECTION_
				using namespace Reflection;
				PTypeNode n = GetGlobleLibrary().GetTypeNodeByInfo(type->GetID());//找到对应节点
				if (n!=null)
				{
					ptr<Object> result = n.DynamicConvertTo(this, from, lv < 2);
					if (result->GetType() != this || (result->GetType() == Type::Make<PMObjects>() && ((PMObjects&)result)->GetInnerType() != this)) {
						ptr<Object> rt = DynamicConvertFrom(result);
						if (rt->GetType() != Type::Make<PMObjects>() || ((PMObjects&)rt)->IsCopy())return rt;
						if (*(void**)MObjects::GetDataPtr(rt) == &result)//引用了局部变量
							return MObjects::MakeMObjectByTypeInfo(&result, (Type*)this);
					}

					return result;
				}
#endif
			}
		}
		else//封箱对象
		{
			//获取封箱描述类型
			type = ((PMObjects&)from)->GetInnerType();
			//若同类型，则进行转换
			if (CompareWithoutExtents(type) == 0)return Type::SameTypeReferenceConvert((Type*)this, ((PMObjects&)from));
			//判断是否基础类型void,bool,s8,s16,i32,s32,s64,u8,u16,ui32,u32,u64,float,double,long double,char,wchar_t
			if (IsPureTypeIgnoreExtents() && type->IsPureTypeIgnoreExtents() && type->GetID() <= 19 && type->GetID() > 1 && ID <= 19 && ID > 1)
			{
#ifdef _CLR
				if (type->GetID() > 17 || ID > 17) {
					return MObjects::MakeMObjectByTypeInfo(((PMObjects&)from).GetObjectPtr(), (Type*)this);
				}
				else {
#endif
					long double d = 0;
					union {
						float fu;
						double du;
						unsigned long long u;
					};
					u = 0;
					if (type->GetID() < 8 || type->GetID() > 15)//有符号整型
					{
						if (type->GetID() == 7) {
							u = *(s64*)((PMObjects&)from).Check()->GetObjectPtr();
							d = (long double)(s64)u;
						}
						if (type->GetID() == 5 || type->GetID() == 6) {//4字节
							u = (s64)*(s32*)((PMObjects&)from).Check()->GetObjectPtr();
							d = (long double)(s32)u;
						}
						else if (type->GetID() == 4 || type->GetID() == 17) {//2字节
							u = (s64)*(s16*)((PMObjects&)from).Check()->GetObjectPtr();
							d = (long double)(s16)u;
						}
						else {
							u = (s64)*(s8*)((PMObjects&)from).Check()->GetObjectPtr();
							d = (long double)(s8)u;
						}
					}
					else if (type->GetID() > 7 && type->GetID() < 13)//无符号整型
					{
						if (type->GetID() == 12)
							u = *(u64*)((PMObjects&)from).Check()->GetObjectPtr();
						else if (type->GetID() == 10 || type->GetID() == 11)
							u = *(u32*)((PMObjects&)from).Check()->GetObjectPtr();
						else if (type->GetID() == 9)
							u = *(u16*)((PMObjects&)from).Check()->GetObjectPtr();
						else if (type->GetID() == 8)
							u = *(u8*)((PMObjects&)from).Check()->GetObjectPtr();
						d = (long double)u;
					}
					else if (type->GetID() == 13)
						u = (unsigned long long)(d = *(float*)((PMObjects&)from).Check()->GetObjectPtr());
					else if (type->GetID() == 14)
						u = (unsigned long long)(d = *(double*)((PMObjects&)from).Check()->GetObjectPtr());
					else if (type->GetID() == 15)
						u = (unsigned long long)(d = *(long double*)((PMObjects&)from).Check()->GetObjectPtr());
					if (ID == 13)
						fu = (float)d;
					else if (ID == 14)
						du = (double)d;
					else if (ID == 15)
						return MObjects::MakeMObjectByTypeInfo(&d, (Type*)this);
					return MObjects::MakeMObjectByTypeInfo(&u, (Type*)this);
#ifdef _CLR
				}
#endif
			}
			//如果被转换类型为指针或数组，目标类型为bool
			if (ID == GetId<bool>() && (type.GetPtr()->props.size() != 0 || type.GetPtr()->funArgs.size() != 0))
			{
				objptr = MObjects::GetObjectPtr(from);
				bool v;
				if (type->IsLValueReference() && !type->IsArray())
				{
					if (!objptr)Throw<PPointerIsNullException >();
					v = *(size_t*)objptr != 0;
				}
				else v = objptr != NULL;
				return MObjects::MakeMObjectByTypeInfo(&v, (Type*)this);
			}
			//先判断是否是指针且存在可转换关系，如果可以则进行转换
			if (IsPointerConvertible(*type.GetPtr(), *this))
			{
				//存放的是实际封箱数据地址
				objptr = MObjects::GetDataPtr(from);
				//执行转换过程
				if ((IsPointer() && IsLValueReference()) == (type->IsPointer() && type->IsReference()))
					return MObjects::MakeMObjectByTypeInfo((Type*)this, objptr);
				if (IsPointer() && IsLValueReference())
					return MObjects::MakeMObjectByTypeInfo((Type*)this, &objptr);//这里并不会发生返回局部变量引用的事件，因为实际上复制的是objptr本身
				return MObjects::MakeMObjectByTypeInfo((Type*)this, *(void**)objptr);
			}
			//当lv>0时才查找转换列表
			else if (lv > 0)
			{
				//再判断如果待转换类型是纯类型（忽略外层特性）则查找其转换列表
				if (type->IsPureTypeIgnoreExtents()) {
#ifdef LK_REFLECTION_
					using namespace Reflection;
					PTypeNode n = GetGlobleLibrary().GetTypeNodeByInfo(type->GetID());
					//若找到对应节点则进入转换过程。若找不到节点，则就算目标类型是纯类型，也不可能在Reflection存在适合的转换过程，将直接抛异常
					if (n!=null)
					{
						ptr<Object> result = n.DynamicConvertTo(this, from, lv < 2);
						if (result->GetType() != this || (result->GetType() == Type::Make<PMObjects>() && ((PMObjects&)result)->GetInnerType() != this)) {
							ptr<Object> rt = DynamicConvertFrom(result);
							if (rt->GetType() != Type::Make<PMObjects>() || ((PMObjects&)rt)->IsCopy())return rt;
							if (*(void**)MObjects::GetDataPtr(rt) == &result)//引用了局部变量
								return MObjects::MakeMObjectByTypeInfo(&result, (Type*)this);
						}

						return result;
					}
#endif
				}
				//否则如果目标类型是纯类型（忽略外层特性）则查找构造列表
				else if (IsPureTypeIgnoreExtents())
				{
#ifdef LK_REFLECTION_
					using namespace Reflection;
					PTypeNode n = GetGlobleLibrary().GetTypeNodeByInfo(ID);
					//由于构造函数可以有多个参数，因此查找的时候需要传入一个数组
					Collection::ReferenceArray<ptr<Object>>arg(1, &from);
					//若找到对应节点则执行转换
					if (n!=null)
					{
						ptr<Object> result = n.DynamicConstructFrom(&arg, lv < 2);
						if (result->GetType() != this || (result->GetType() == Type::Make<PMObjects>() && ((PMObjects&)result)->GetInnerType() != this))
						{
							ptr<Object> rt = DynamicConvertFrom(result);
							if (rt->GetType() != Type::Make<PMObjects>() || ((PMObjects&)rt)->IsCopy())return rt;
							if (*(void**)MObjects::GetDataPtr(rt) == &result)//引用了局部变量
								return MObjects::MakeMObjectByTypeInfo(&result, (Type*)this);
						}
						return result;
					}
#endif
				}
			}
		}
		//万不得已允许转换到ptr<Object>
		if (lv > 1 && CompareWithoutExtents(Type::Make<ptr<Object>>()) == 0)
		{
			if (IsLValueReference())return MObjects::MakeMObjectByTypeInfo((Type*)this, &from);
			return from;
		}
		//无法转换，抛出异常
		return RThrow<ptr<Object>, PTypesConvertErrorException>(L"无法将类型：" + type->GetFullName() + L" 转换到类型：" + GetFullName());
	}
	bool Type::CanDynamicConvertFrom(PType const&type, index_t lv)const
	{
		//如果类型相同，则进行基本转换
		if (CompareWithoutExtents(type) == 0)return true;
		//判断是否基础类型void,bool,s8,s16,i32,s32,s64,u8,u16,ui32,u32,u64,float,double,long double,char,wchar_t
		if (IsPureTypeIgnoreExtents() && type->IsPureTypeIgnoreExtents()&&type->GetID() <= 19 && type->GetID() > 1 && ID <= 19 && ID > 1){
#ifdef _CLR
			if (type->GetID() > 17 && ID > 17){
				return type.Check()->gctype->IsAssignableFrom(gctype);
			}
			if(type->GetID() <=17&&ID <=17)
#endif
			return true;
		}
		//如果被转换类型为指针，目标类型为bool
		if (ID == GetId<bool>() && type.GetPtr()->props.size() != 0)return true;
		//先判断是否是指针且存在可转换关系，如果可以则进行转换
		if (IsPointerConvertible(*type.GetPtr(), *this))return true;
		//当lv>0时才查找转换列表
		if (lv > 0)
		{
			//再判断如果待转换类型是纯类型（忽略外层特性）则查找其转换列表
			if (type->IsPureTypeIgnoreExtents()) {
#ifdef LK_REFLECTION_
				using namespace Reflection;
				PTypeNode n = GetGlobleLibrary().GetTypeNodeByInfo(type->GetID());
				//若找到对应节点则进入转换过程。若找不到节点，则就算目标类型是纯类型，也不可能在Reflection存在适合的转换过程，将直接抛异常
				if (n!=null && n.CanConvertTo(type, lv < 2))return true;
#endif
			}
			//否则如果目标类型是纯类型（忽略外层特性）则查找构造列表
			else if (IsPureTypeIgnoreExtents())
			{
#ifdef LK_REFLECTION_
				using namespace Reflection;
				PTypeNode n = GetGlobleLibrary().GetTypeNodeByInfo(ID);
				//由于构造函数可以有多个参数，因此查找的时候需要传入一个数组
				Collection::ReferenceArray<PType>arg(1, &type);
				//若找到对应节点则执行转换
				if (n!=null && n.CanConstructFrom(&arg, lv < 2))return true;
#endif
			}
		}
		//万不得已允许转换到ptr<Object>
		if (lv > 1 && CompareWithoutExtents(Type::Make<ptr<Object>>()) == 0)return true;
		//无法转换
		return false;
	}

	PType Type::GetType()const { return Type::Make<Type>(); }
	Type* Type::Clone()const { return new Type(*this); }
	/**
	@brief 转变为指向该类型的指针类型，cnt表示指针层级，默认为1;ar为外层限定特性，默认为0*/
	Type& Type::SelfToPointer(index_t cnt, size_t at)
	{
		if (cnt < 0)Throw<PInvalidArgumentException>();
		if (props.size() == 0 || props.back()>0)
		{
			index_t i = (index_t)props.size();
			index_t len = (cnt * 2 + sizeof(index_t) * 8 - 1) / (sizeof(index_t) * 8);
			props.resize(i + len + 2);
			props[i] = props[i + len + 1] = -cnt;
			while (len > 1)props[i + len--] = 0;
			props[i + 1] = attr & 3;//忽略引用特性
		}
		else
		{
			index_t len = (sizeof(index_t) * 8 - 1 - props.back() * 2);
			index_t i = props.size() - 2 - (len) / (sizeof(index_t) * 8);
			props.back() = 0;
			props.resize(i + 2 + (len + cnt * 2) / (sizeof(index_t) * 8));
			props.back() = props[i] - cnt;
			len -= sizeof(index_t) * 8 - 1;
			props[i + 1 + len / (sizeof(index_t) * 8)] |= (attr & 3) << (len&(sizeof(index_t) * 8 - 1));
			props[i] = props.back();
		}
		attr = at;
		return *this;
	}
	/**
	@brief 转变为该类型的数组类型，cnt表示该维元素数*/
	Type& Type::SelfToArray(index_t cnt)
	{
		if (cnt <= 0)Throw<PInvalidArgumentException>();
		props.resize(props.size() + 1);
		props.back() = cnt;
		return *this;
	}
	/**
	@brief 转变为该类型解引用的类型，cnt表示解引用次数，默认为1*/
	Type& Type::SelfToPointed(index_t cnt)
	{
		while (cnt > 0)
		{
			if (props.size() == 0)break;
			if (props.back() < 0)
			{
				index_t i = ((-props.back()) + 8 * sizeof(index_t) - 1) / (8 * sizeof(index_t));
				props.back()++;
				attr = Bits::GetX(&props[props.size() - 1 - i], -props.back() * 2) & 0x3;
				Bits::Set(&props[props.size() - 1 - i], -props.back() * 2, 2, false);
				props[props.size() - 2 - i]++;
				if ((index_t)(((-props.back()) + 8 * sizeof(index_t) - 1) / (8 * sizeof(index_t))) < i)
				{
					props[props.size() - 2] = props.back();
					props.resize(props.size() - 1);
				}
				if (props.back() == 0)props.resize(props.size() - 2);
			}
			else props.resize(props.size() - 1);
			--cnt;
		}
		return *this;
	}
	/**
	@brief 设置函数类型*/
	void Type::SetFunctionArgs(std::vector<PType>types)
	{
		funArgs = types;
		if (funArgs[1]->GetID() != Type::GetId<void>())
			ID = funArgs[1]->GetID();
		funArgs.erase(funArgs.begin() + 1);
	}
	/**
	@brief 获取数组维数*/
	index_t Type::GetArrayLevel()const
	{
		if (props.size() == 0)return 0;
		for (std::vector<index_t>::const_reverse_iterator it = props.rbegin(); it != props.rend(); ++it)
			if (*it < 0)return it - props.rbegin();
		return props.size();
	}
	/**
	@brief 获取数组指定维上的元素数，比如对于数组类型int[5][4]，传入参数为0时返回5*/
	index_t Type::GetElementCount(index_t d)const
	{
		if (props.size() == 0 || d < 0)return 0;
		if (props.back() < 0)return 0;
		for (index_t i = (index_t)props.size(); i-- > 0;)
		{
			if (props[i] < 0)return 0;
			if (d-- == 0)return props[i];
		}
		return 0;
	}
	/**
	@brief 获取全称*/
	PString Type::GetFullName()const { return GetFullName(L""); }
	/**
	@brief 获取全称*/
	PString Type::GetName()const { return GetName(L""); }
	/**
	@brief 获取全称*/
	PString Type::GetFullName(PString str)const
	{
		u8 proarr = 0;
		if (attr != 0)//先得到外部描述
		{
			if (IsLValueReference())
			{
				if (IsGCHandle())
					str = L"%" + str;
				else
					str = L"&" + str;
			}
			else if (IsRValueReference())str = L"&&" + str;
			if (!IsArray() && !(ID != 1 && IsFunction()))
			{
				if (IsVolatile())str = L" volatile" + str;
				if (IsConst())str = L" const" + str;
			}
			else proarr = u8(attr & 0x3);
		}
		if (props.size() != 0)//再得到指针和数组描述
		{
			index_t i = props.size() - 1;
			for (;;)
			{
				if (props[i] < 0)//指针
				{
					index_t cnt = -props[i];
					i -= ((-props[i]) * 2 + sizeof(index_t) * 8 - 1) / (sizeof(index_t) * 8);
					while (cnt > 0)
					{
						cnt--;
						str = L"*" + str;
						if (cnt == 0 && (i > 1 || (ID == 1 && funArgs.size() != 0)))proarr = u8(props[i] & 0x3);
						else {
							if (Bits::Get(&props[i], cnt * 2 + 1))//volatile
								str = L" volatile" + str;
							if (Bits::Get(&props[i], cnt * 2))//const
								str = L" const" + str;
						}
					}
					if ((i -= 2) < 0)break;
				}
				if (!str.IsNullOrEmpty())str = L"(" + str + L")";
				PString xstr = PString::GetEmpty();
				while (i >= 0 && props[i] > 0)//数组
				{
					xstr += L"[" + PString(props[i]) + L"]";
					i--;
				}
				str += xstr;
				if (proarr != 0)
				{
					if ((proarr&TypeExtents::Volatile) != 0)//volatile
						str = L" volatile" + str;
					if ((proarr&TypeExtents::Const) != 0)//const
						str = L" const" + str;
					proarr = 0;
				}
				if (i < 0)break;
			}
		}
		if (funArgs.size() != 0 && ID != 1)
			str = L"::*" + str;
#ifdef _CLR
		if (ID == 18) {
			str = ("gcroot<" + gctype->FullName + "^>") + str;
		}
		else if (ID == 19) {
			str = (gctype->FullName + "^") + str;
		}
		else
#endif
		str = LK::Reflection::GetFullClassNameById(ID) + str;
		//最后是函数描述
		if (funArgs.size() != 0)
		{
			if (!str.IsNullOrEmpty())
				str = L"(" + str + L")";
			str += L"(";
			for (index_t i = 1; i < (index_t)funArgs.size(); ++i)
			{
				if (funArgs[i]==null)
				{
					str += L"...";
					break;
				}
				if (i != 1)str += L",";
				str += funArgs[i]->GetFullName();
			}
			str += L")";
			if ((proarr&TypeExtents::Const) != 0)str += L" const";
			str = funArgs[0]->GetFullName(str);
		}
		return str;
	}
	/**
	@brief 获取名称*/
	PString Type::GetName(PString str)const
	{
		u8 proarr = 0;
		if (attr != 0)//先得到外部描述
		{
			if (IsLValueReference())
			{
				if(IsGCHandle())str = L"%" + str;
				else str = L"&" + str;
			}
			else if (IsRValueReference())str = L"&&" + str;
			if (!IsArray() && !(ID != 1 && IsFunction()))
			{
				if (IsVolatile())str = L" volatile" + str;
				if (IsConst())str = L" const" + str;
			}
			else proarr = u8(attr & 0x3);
		}
		if (props.size() != 0)//再得到指针和数组描述
		{
			index_t i = props.size() - 1;
			for (;;)
			{
				if (props[i] < 0)//指针
				{
					index_t cnt = -props[i];
					i -= ((-props[i]) * 2 + sizeof(index_t) * 8 - 1) / (sizeof(index_t) * 8);
					while (cnt > 0)
					{
						cnt--;
						str = L"*" + str;
						if (cnt == 0 && (i > 1 || (ID == 1 && funArgs.size() != 0)))proarr = u8(props[i] & 0x3);
						else {
							if (Bits::Get(&props[i], cnt * 2 + 1))//volatile
								str = L" volatile" + str;
							if (Bits::Get(&props[i], cnt * 2))//const
								str = L" const" + str;
						}
					}
					if ((i -= 2) < 0)break;
				}
				if (!str.IsNullOrEmpty())str = L"(" + str + L")";
				PString xstr = PString::GetEmpty();
				while (i >= 0 && props[i] > 0)//数组
				{
					xstr += L"[" + PString(props[i]) + L"]";
					i--;
				}
				str += xstr;
				if (proarr != 0)
				{
					if ((proarr&TypeExtents::Volatile) != 0)//volatile
						str = L" volatile" + str;
					if ((proarr&TypeExtents::Const) != 0)//const
						str = L" const" + str;
					proarr = 0;
				}
				if (i < 0)break;
			}
		}
		if (funArgs.size() != 0 && ID != 1)
			str = L"::*" + str;
#ifdef _CLR
		if (ID == 18){
			str = ("gcroot<" + gctype->FullName + "^>") + str;
		}
		else if(ID==19) {
			str = (gctype->FullName + "^") + str;
		}
		else
#endif
		str = LK::Reflection::GetClassNameById(ID) + str;
		//最后是函数描述
		if (funArgs.size() != 0)
		{
			if (!str.IsNullOrEmpty())
				str = L"(" + str + L")";
			str += L"(";
			for (index_t i = 1; i < (index_t)funArgs.size(); ++i)
			{
				if (funArgs[i]==null)
				{
					str += L"...";
					break;
				}
				if (i != 1)str += L",";
				str += funArgs[i]->GetName();
			}
			str += L")";
			if ((proarr&TypeExtents::Const) != 0)str += L" const";
			str = funArgs[0]->GetName(str);
		}
		return str;
	}
	/**
	@brief 判断该类型的原始类型是否从另一类型派生*/
	bool Type::IsDerivedFrom(PType const& t)const
	{
#ifdef _CLR
		if (t->GetID() == 19 || ID == 19){
			return t.Check()->gctype->IsAssignableFrom(gctype);
		}
#endif
		if (ID == t->GetID())return true;
#ifdef LK_REFLECTION_
		using namespace Reflection;
		PTypeNode n = GetGlobleLibrary().GetTypeNodeByInfo(ID);
		if (n==null)return false;
		return n.IsDerivedFrom(GetGlobleLibrary().GetTypeNodeByInfo(t->GetID()));
#endif
		return false;
	}

	std::vector<CommonTypeFunctionInfoStruct>& Type::GetTypeCommonInfoRef()
	{
		static std::vector<CommonTypeFunctionInfoStruct> r;
		if (r.size() == 0)
		{
			SpinMutex::GetDefaultRef().Lock();
			if (r.size() == 0) {
				//17个基本数据类型void,bool,s8,s16,i32,s32,s64,u8,u16,ui32,u32,u64,float,double,long double,char,wchar_t
				r.resize(20);
				r[2].size = r[3].size = r[8].size = 1;
				r[4].size = r[9].size = 2;
				r[5].size = r[10].size = sizeof(int);
				r[6].size = r[11].size = r[13].size = 4;
				r[7].size = r[12].size = r[14].size = 8;
				r[15].size = sizeof(long double);
				r[16].size = sizeof(char);
				r[17].size = sizeof(wchar_t);
				r[18].size = sizeof(size_t);
				r[19].size = sizeof(size_t);
				r[1].GetNameRef() = L"void"; r[2].GetNameRef() = L"bool";
				r[3].GetNameRef() = L"signed char"; r[4].GetNameRef() = L"short"; r[5].GetNameRef() = L"int"; r[6].GetNameRef() = L"long"; r[7].GetNameRef() = L"long long";
				r[8].GetNameRef() = L"unsigned char"; r[9].GetNameRef() = L"unsigned short"; r[10].GetNameRef() = L"unsigned int"; r[11].GetNameRef() = L"unsigned long"; r[12].GetNameRef() = L"unsigned long long";
				r[13].GetNameRef() = L"float"; r[14].GetNameRef() = L"double"; r[15].GetNameRef() = L"long double"; r[16].GetNameRef() = L"char"; r[17].GetNameRef() = L"wchar_t";
				r[18].GetNameRef() = L"gcroot<...>"; r[19].GetNameRef() = L"gctype^";
			}
			SpinMutex::GetDefaultRef().UnLock();
		}
		return r;
	}
	void Type::CallAddTypeInfoFunction(void(*p)())
	{
		static ptr<Collection::IArray<void(*)()>> arr((index_t)0);
		index_t i = arr->DichotomyIndexOf(p);
		if (i < 0)
		{
			arr->DichotomyInsertAsc(p);
			p();
		}
	}
	bool Type::IsPointerConvertible(Type const&from, Type const&to)
	{
		if (&from == &to)return true;
		if (from.props.size() != 0 && to.IsPointer() && from.funArgs == to.funArgs)//指针的转换
		{
			bool cando = to.ID == GetId<void>() && to.props.size() == 3 && to.props[0] == -1;//转换到void 指针总是被允许的
			if (!cando)//有转换的可能
			{
				cando = true;
				PType t1 = from.Clone();
				PType t2 = to.Clone();
				t1->SelfToPointed();
				t2->SelfToPointed();
				//比较所指向内容const关系
				if (t1.GetPtr()->props.size() != t2.GetPtr()->props.size())cando = false;
				else
				{
					index_t i = 0;
					for (; i < (index_t)t1.GetPtr()->props.size(); ++i)
					{
						if (t1.GetPtr()->props[i] < 0)
						{
							index_t k = i + (sizeof(index_t) * 8 - 1 - 2 * t1.GetPtr()->props[i]) / (sizeof(index_t) * 8);
							if (t1.GetPtr()->props[i] == t2.GetPtr()->props[i])while (i < k)
							{
								++i;
								if (((t1.GetPtr()->props[i] ^ t2.GetPtr()->props[i])&t1.GetPtr()->props[i]) != 0)break;
							}
							if (i < k) { cando = false; break; }
							i += 2;
						}
						else if (t1.GetPtr()->props[i] != t2.GetPtr()->props[i]) { cando = false; break; }
					}
					if (i == (index_t)t1.GetPtr()->props.size() && ((t1.GetPtr()->attr^t2.GetPtr()->attr)&t1.GetPtr()->attr) != 0)cando = false;
					//比较继承关系
					if (cando)
					{
						if (t1->GetID() != t2->GetID())
						{
							cando = false;
#ifdef LK_REFLECTION_
							using namespace Reflection;
							PTypeNode n = GetGlobleLibrary().GetTypeNodeByInfo(t1->GetID());
							PTypeNode b = GetGlobleLibrary().GetTypeNodeByInfo(t2->GetID());
							if (n!=null && b!=null)cando = n.IsDerivedFrom(b);
#endif
						}
					}
				}
			}
			return cando;
		}
		return false;
	}
	/**
	@breif 对数据地址p使用该类型的默认构造函数构造*/
	void Type::DefaultConstruct(void* p)const
	{
		if (!p)Throw<PPointerIsNullException >();
		//对于引用、数组、指针、函数的操作：
		if (0 != (attr&TypeExtents::LValueReference)) { *(size_t*)p = 0; return; }
		DefaultConstructIgnoreReference(p);
	}
	/**
	@breif 对数据地址p使用该类型的析构函数析构*/
	void Type::Deconstruct(void* p)const
	{
		if (!p)Throw<PPointerIsNullException >();
		//对于引用、数组、指针、函数的操作：
		if (0 != (attr&TypeExtents::LValueReference))  return;
		DeconstructIgnoreReference(p);
	}
	/**
	@breif 使用数据地址ps的内容使用该类型的复制构造方法写到数据地址pd中*/
	void Type::CopyConstruct(void* pd, void const*ps)const
	{
		if (!ps || !pd)Throw<PPointerIsNullException >();
		//对于引用、数组、指针、函数的操作：
		if (0 != (attr&TypeExtents::LValueReference))
		{
			*(size_t*)pd = *(const size_t*)ps;
			return;
		}
		CopyConstructIgnoreReference(pd, ps);
	}
	/**
	@breif 对数据地址p使用该类型的默认构造函数构造（忽略引用特性）*/
	void Type::DefaultConstructIgnoreReference(void* p)const
	{
		if (!p)Throw<PPointerIsNullException >();
		if (ID < 17)return;
#ifdef _CLR
		if (ID == 18 || ID == 19) { placementnew((gcroot<System::Object^>*)p); return; }
#endif
		//对于数组、指针、函数的操作：
		if (props.size() != 0 || funArgs.size() != 0) { *(size_t*)p = 0; return; }
		//其它静态类型查找并调用静态的构造函数
		if (ID > 0)return GetTypeCommonInfoRef()[ID].DefaultConstructMethod(p);
		//动态类型未定义
		if (ID < 0) { Throw<PException >(L"动态类型析构函数未定义"); }
	}
	/**
	@breif 对数据地址p使用该类型的析构函数析构（忽略引用特性）*/
	void Type::DeconstructIgnoreReference(void* p)const
	{
		if (!p)Throw<PPointerIsNullException >();
		//对于数组、指针、函数的操作：
		if (props.size() != 0 || funArgs.size() != 0 || ID < 17) { return; }
#ifdef _CLR
		if (ID == 18 || ID == 19) { *(gcroot<System::Object^>*)p=(System::Object^)nullptr; return; }
#endif
		//其它静态类型查找并调用静态的析构函数
		if (ID > 0)return GetTypeCommonInfoRef()[ID].DeconstructMethod(p);
		//动态类型未定义
		if (ID < 0) { Throw<PException >(L"动态类型析构函数未定义"); }
	}
	/**
	@breif 使用数据地址ps的内容使用该类型的复制构造方法写到数据地址pd中（忽略引用特性）*/
	void Type::CopyConstructIgnoreReference(void* po, void const*ps)const
	{
		if (!ps || !po)Throw<PPointerIsNullException >();
		//对于数组、指针、函数的操作：
		if (props.size() != 0 || funArgs.size() != 0)
		{
			*(size_t*)po = *(const size_t*)ps;
			return;
		}
		if (ID <= 19)
		{//void,bool,s8,s16,i32,s32,s64,u8,u16,ui32,u32,u64,float,double,long double,char,wchar_t
			switch (ID)
			{
			case 2:case 3:case 8:*(u8*)po = *(const u8*)ps; break;
			case 4:case 9:*(u16*)po = *(const u16*)ps; break;
			case 5:case 10:*(ui32*)po = *(const ui32*)ps; break;
			case 6:case 11:case 13:*(u32*)po = *(const u32*)ps; break;
			case 7:case 12:case 14:*(u64*)po = *(const u64*)ps; break;
			case 15:*(long double*)po = *(const long double*)ps; break;
			case 16:*(char*)po = *(const char*)ps; break;
			case 17:*(wchar_t*)po = *(const wchar_t*)ps;
#ifdef _CLR
			case 18:
			case 19:placementnew((gcroot<System::Object^>*)po, *(const gcroot<System::Object^>*)ps);
#endif
			default:break;
			}
			return;
		}
		//其它静态类型查找并调用静态的复制构造函数
		if (ID > 0)return GetTypeCommonInfoRef()[ID].CopyConstructMethod(po, ps);
		//动态类型未定义
		if (ID < 0) { Throw<PException >(L"动态类型复制构造函数未定义"); }
	}
}
namespace LK
{
	namespace Reflection
	{
#ifndef LK_REFLECTION_
		PString GetFullClassNameById(index_t id)
		{
			return GetClassNameById(id);
		}
		PString GetClassNameById(index_t id)
		{
			if (id < 0)return L"dynamic" + PString(-id);
			return Type::GetTypeCommonInfoRef()[id].GetNameRef();
		}
#endif
	}
}
#endif
